<?php
/*
 *      stpw_php.php
 *      
 *      Copyright 2011 Diego Rivas <diego@myhost>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 *      
 *      
 */

$textos = Array("quijote.txt", "fierro.txt", "tenorio.txt", "cuatrojinetes.txt", "enciclopedia.txt", "lacelestina.txt", "cuentosdeamor.txt", 
				"facundo.txt", "sangreyarena.txt", "memoriasvigilante.txt", "panycebolla.txt", "diablocojuelo.txt", "aldeaperdida.txt", 
				"entrenaranjos.txt", "elintruso.txt", "lacondenada.txt");

$dicc = Array();
foreach($textos as $texto){

	$file = file_get_contents($texto);

	$lineas = explode("\n", $file);
	unset($file);



	foreach ($lineas as $linea){
		$palabras = explode(" ", $linea);
		
		foreach ($palabras as $palabra){
			$palabra = filtrar($palabra);
			
			if ($palabra){
				if (isset($dicc[$palabra])){
					$dicc[$palabra] += 1;
				}else{
					$dicc[$palabra] = 1;
				}
			}
		}
	}
	unset($lineas);
}

arsort($dicc);

$total = 0;

foreach ($dicc as $key => $value){
	$total += $value;
}

$out = fopen("salidaphp.txt", "w");

fwrite($out, "\n\nTerminos: $total\n");
fwrite($out, "Terminos distintos: ".count($dicc)."\n\n");

foreach ($dicc as $key => $value){
	$fraccion = ($value / $total) * 100;
	fwrite($out, "$key => $value => $fraccion\n");
}

fclose($out);




function filtrar($palabra){
	$palabra = strtolower($palabra);
	
	$palabra = str_ireplace(" ", "", $palabra);
	$palabra = str_ireplace("\n", "", $palabra);
	$palabra = str_ireplace("\b", "", $palabra);
	$palabra = str_ireplace("\r", "", $palabra);
	$palabra = str_ireplace(",", "", $palabra);
	$palabra = str_ireplace(".", "", $palabra);
	$palabra = str_ireplace(";", "", $palabra);
	$palabra = str_ireplace(":", "", $palabra);
	$palabra = str_ireplace("-", "", $palabra);
	$palabra = str_ireplace("\t", "", $palabra);
	$palabra = str_ireplace("*", "", $palabra);
	$palabra = str_ireplace("+", "", $palabra);
	$palabra = str_ireplace("-", "", $palabra);
	$palabra = str_ireplace("(", "", $palabra);
	$palabra = str_ireplace(")", "", $palabra);
	$palabra = str_ireplace("{", "", $palabra);
	$palabra = str_ireplace("}", "", $palabra);
	$palabra = str_ireplace("[", "", $palabra);
	$palabra = str_ireplace("]", "", $palabra);
	$palabra = str_ireplace("?", "", $palabra);
	$palabra = str_ireplace("¿", "", $palabra);
	$palabra = str_ireplace("_", "", $palabra);
	$palabra = str_ireplace("\"", "", $palabra);
	$palabra = str_ireplace("á", "a", $palabra);
	$palabra = str_ireplace("é", "e", $palabra);
	$palabra = str_ireplace("í", "i", $palabra);
	$palabra = str_ireplace("ó", "o", $palabra);
	$palabra = str_ireplace("ú", "u", $palabra);
	$palabra = str_ireplace("'", "", $palabra);
	$palabra = str_ireplace("/", "", $palabra);
	$palabra = str_ireplace("\\", "", $palabra);
	
	return $palabra;
}


?>
